<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";


class HistoryLinksSnippet extends IPlugin 
{
    function HistoryLinksSnippet($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
    }

    function getPluginTag() { return "historylinks"; }
    function getPluginName() { return "Create History links in tree-data"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function Render($params = null)
    {


        $core = getCoreInstance();
        $render = $core->getProperty("framework"); $render = $render->smarty;
        $db = $core->getProperty("db");

        $render->assign("hslnk_root", $params["html_root"]);
        $render->assign("hslnk_root_url", $params["html_url"]);
        $render->assign("hslnk_separator", $params["html_separatop"]);
        
        $stop = $params["stopval"];
        $id = $params["f_id"];
        $parent = $params["f_parent"];
        $title = $params["f_title"];
        $table = $params["table"];
        
        $history = array();
        $sql = "select {$id} as id, {$parent} as parent, {$title} as title from {$table} where {$id} = '{$params[startfrom]}'";
        $r = ArrayDb2Str($db->getAll($sql));
        if(count($r)>0)
        {
            $history[] = array($r[0]["title"], str_replace("###", $r[0]["id"] ,$params["url"]) );
        }

        while(count($r)>0 && $r[0]["parent"]!=$stop)
        {
            $sql = "select {$id} as id, {$parent} as parent, {$title} as title from {$table} where {$id} = '{$r[0][parent]}'";
            $r = ArrayDb2Str($db->getAll($sql));
            $history[] = array($r[0]["title"], str_replace("###", $r[0]["id"], $params["url"]));
        }
        $history[] = array($params["html_root"], $params["html_url"]);
        $history = array_reverse($history);
        if(!isset($params['is_last_link']) or $params['is_last_link']=='N'){
            $history[count($history)-1][1] = "";
        }

        $render->assign("history", $history);
        $result = $render->fetch("file:".$this->tplfolder."/history.tpl.php");
        
        return $result;
    }
};


?>